<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="80%" :scroll="true" :maxHeight="550">
    <el-button
      type="primary"
      plain
      @click="handleExport"
      :loading="exportLoading"
      v-hasPermi="['wms:inventory-record:export']"
      :disabled="isEmpty(list)"
    >
      <Icon icon="ep:download" class="mr-5px" /> 导出 Excel
    </el-button>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="商品" align="center" prop="name" />
      <el-table-column label="数量" align="center" prop="count" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="备注" align="center" prop="description" />
      <el-table-column label="创建时间" align="center" prop="createAt" />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </Dialog>
</template>
<script setup lang="ts">
import { InventoryRecordApi, InventoryRecordVO } from '@/api/wms/inventoryrecord'
import download from '@/utils/download'
import { isEmpty } from "@/utils/is";

/** 库存记录 表单 */
defineOptions({ name: 'InventoryRecordForm' })

// const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题

/** 打开弹窗 */
const openTable = (type: string, id?: string) => {
  dialogVisible.value = true
  dialogTitle.value = '库存记录'
  console.log('openTable:', type, id)

  // 通过wid获取库存记录
  queryParams.wid = id as string
  getList()
}
defineExpose({ openTable }) // 提供 open 方法，用于打开弹窗

const exportLoading = ref(false) // 导出的加载中
/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await InventoryRecordApi.exportInventoryRecord(queryParams)
    download.excel(data, '出入库记录.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const loading = ref(true) // 列表的加载中
const list = ref<InventoryRecordVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  wid: '', // 仓库id
})
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await InventoryRecordApi.getInventoryRecordPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/*onMounted(() => {
  getList()
})*/
</script>
